/*
 * $Id: MoveDeleteHook.java 67 2002-11-03 18:37:46Z bpruessmann $
 * 
 * Copyright (c) 2002 David Corbin and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    David Corbin - Initial implementation.
 */
package net.sourceforge.perforce.ui.internal;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.internal.P4TeamProvider;
import net.sourceforge.perforce.core.resources.LocalFile;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.team.IMoveDeleteHook;
import org.eclipse.core.resources.team.IResourceTree;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.preference.IPreferenceStore;

/**
  * Implements IMoveDeleteHook, so refactoring of classes in source control will
  * work correctly.  Currently works only for simple Java class rename.
  * 
  * @see org.eclipse.core.resources.team.IMoveDeleteHook
  * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  *
  */
public class MoveDeleteHook implements IMoveDeleteHook 
{
	private P4TeamProvider provider;
	
	public MoveDeleteHook(P4TeamProvider provider)
	{
		this.provider = provider;
	}
	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteFile(IResourceTree, IFile, int, IProgressMonitor)
	 */
	public boolean deleteFile(
		IResourceTree tree,
		IFile file,
		int updateFlags,
		IProgressMonitor monitor) 
	{
		IPreferenceStore store = PerforceUIPlugin.getPlugin().getPreferenceStore();
		if (store.getBoolean(PerforceUIPlugin.DELETE_AFFECTS_CHANGELIST))
		{
			LocalFile localSource = (LocalFile)LocalFile.createFromResource(file);

			// If the local file is opened for ... we have to revert it
			if (localSource.isCheckedOut())
			{
				try
				{
					localSource.revert();
				}
				catch (PerforceException e)
				{
					PerforceCorePlugin.log(e.getStatus());
					return true;
				}
			}
			
			// If the file is now under perforce control we have to open it for 
			// delete.			
			if (localSource.hasRemote())
			{
				try
				{
					localSource.delete();

					tree.standardDeleteFile(file,updateFlags,monitor);
					return true;
				}
				catch (PerforceException e)
				{
					PerforceCorePlugin.log(e.getStatus());
					return true;
				}
			}
		}
		
		return false;
	}

	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteFolder(IResourceTree, IFolder, int, IProgressMonitor)
	 */
	public boolean deleteFolder(
		IResourceTree tree,
		IFolder folder,
		int updateFlags,
		IProgressMonitor monitor) {
		return false;
	}

	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#deleteProject(IResourceTree, IProject, int, IProgressMonitor)
	 */
	public boolean deleteProject(
		IResourceTree tree,
		IProject project,
		int updateFlags,
		IProgressMonitor monitor) {
		return false;
	}

	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveFile(IResourceTree, IFile, IFile, int, IProgressMonitor)
	 */
	public boolean moveFile(
		IResourceTree tree,
		IFile source,
		IFile destination,
		int updateFlags,
		IProgressMonitor monitor) 
	{
		LocalFile localSource = (LocalFile)LocalFile.createFromResource(source);
		if (localSource.hasRemote())
		{
			provider.getPerforceServer().moveFile(tree,source,destination,monitor);
			return true;
		}
		return false;
	}

	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveFolder(IResourceTree, IFolder, IFolder, int, IProgressMonitor)
	 */
	public boolean moveFolder(
		IResourceTree tree,
		IFolder source,
		IFolder destination,
		int updateFlags,
		IProgressMonitor monitor) {
		return false;
	}

	/**
	 * @see org.eclipse.core.resources.team.IMoveDeleteHook#moveProject(IResourceTree, IProject, IProjectDescription, int, IProgressMonitor)
	 */
	public boolean moveProject(
		IResourceTree tree,
		IProject source,
		IProjectDescription description,
		int updateFlags,
		IProgressMonitor monitor) 
		{
		return false;
	}

}

